package com.slj.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slj.comm.responce.PageResult;
import com.slj.comm.responce.R;
import com.slj.model.dto.SuppliersDTO;
import com.slj.model.entity.Suppliers;
import com.slj.service.SuppliersService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 供应商表 前端控制器
 * </p>
 *
 * @author 礼俊
 * @since 2025-09-20 22:46
 */
@RestController
@RequestMapping("/suppliers")
public class SuppliersController {
    @Autowired
    private SuppliersService suppliersService;
    //查询供应商
    @PostMapping("/getSupplierList")
    public R<List<Suppliers>> getSupplierList(){
        return R.success(suppliersService.list()) ;
    }
    //分页查询供应商
    @PostMapping("/getSupplierPage")
    public R<PageResult<Suppliers>> getSupplierPage(@RequestBody SuppliersDTO suppliersDTO){
        Page<Suppliers> page = new Page<>(suppliersDTO.getPageNum(), suppliersDTO.getPageSize());
        LambdaQueryWrapper<Suppliers> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(suppliersDTO.getSupplierName()),Suppliers::getSupplierName, suppliersDTO.getSupplierName());
        queryWrapper.like(StringUtils.isNotBlank(suppliersDTO.getContactName()),Suppliers::getContactName, suppliersDTO.getContactName());
        queryWrapper.like(StringUtils.isNotBlank(suppliersDTO.getContactPhone()),Suppliers::getContactPhone, suppliersDTO.getContactPhone());
        Page<Suppliers> pageData = suppliersService.page(page, queryWrapper);
        PageResult<Suppliers> pageResult = PageResult.of(pageData);
        return R.success(pageResult);
    }
    //添加供应商
    @PostMapping("/addSupplier")
    public R<Boolean> addSupplier(Suppliers suppliers){
        return R.success(suppliersService.save(suppliers));
    }
    //根据id查询供应商
    @GetMapping("/getSupplierById/{id}")
    public R<Suppliers> getSupplierById(@PathVariable Integer id){
        Suppliers suppliers = suppliersService.getById( id);
        return R.success(suppliers);
    }

    //编辑员工
    @PostMapping("/editSupplier")
    public R editSupplier(@RequestBody Suppliers suppliers){
        Integer supplierId = suppliersService.editSupplier(suppliers);
        return  supplierId > 0 ? R.success(true) : R.fail("操作失败");
    }

    //重置密码
    @PostMapping("/resetPassword/{id}")
    public R resetPassword(Integer id){
        Suppliers suppliers = new Suppliers();
        suppliers.setSupplierId(id);
        suppliers.setPassword("123456");
        return suppliersService.updateById(suppliers) ? R.success(true) : R.fail("操作失败");
    }

    //根据id删除供应商
    @PostMapping("/deleteSupplier/{id}")
    public R deleteSupplier(@PathVariable Integer id){
        return suppliersService.removeById(id) ? R.success(true) : R.fail("操作失败");
    }
}
